package com.whu.waterworks.service;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.whu.waterworks.dao.HisWaterPlantDAO;
import com.whu.waterworks.dao.RealWaterPlantDao;
import com.whu.waterworks.dto.WaterPlantDto;
import com.whu.waterworks.model.TWaterplantdata;
import com.whu.waterworks.model.TWaterplantdataHis;
import com.whu.waterworks.util.DateUtil;
import com.whu.waterworks.util.GetObjectName;

/**
 * 水厂数据相关service
 * @author wangying
 *
 */
public class WaterPlantDataService {
	
	public RealWaterPlantDao realWaterPlantDao;
	public HisWaterPlantDAO hisWaterPlantDAO;
	/**
	 * 指定水厂的最新一条数据
	 * @author wangying
	 * @return
	 */
	public TWaterplantdata getLastWaterPlantData(String code)
	{
		TWaterplantdata waterplantdata = realWaterPlantDao.getLastData(code);
		return waterplantdata;
	}
	/**
	 * 指定水厂的实时数据
	 * @author wangying
	 * @param code
	 * @return
	 */
	public List<TWaterplantdata> getWaterplantdatasById(String code)
	{
		List<TWaterplantdata> waterplantdatas = realWaterPlantDao.getWaterplantdatasById(code);
		return waterplantdatas;
	}
	
	/**
	 * 根据时间段得到指定id的指定类型数据
	 * @author wangying
	 * @param id
	 * @param type
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public List<TWaterplantdata> getWaterplantdatasByTwoTime(String id,String beginTime,String endTime)
	{
		List<TWaterplantdata> resutDatas = new ArrayList<>();
		String begin = beginTime+"/"+"00:00:00";
		String end = endTime +"/"+"24:00:00";
		//DateUtil dateUtil = new DateUtil();
		//SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		/*Date endDate = format.parse(endTime);
		if(dateUtil.isToday(endDate))//结束时间为今天，则涉及历史表和实时表
		{
			List<TWaterplantdata> realtimedatas = realWaterPlantDao.getWaterplantdatasById(id);
			if(realtimedatas!=null && realtimedatas.size()>0)
			{
				System.out.println("Data Service realtimedatas.size()="+realtimedatas.size());
				resutDatas.addAll(realtimedatas);
			}
			List<TWaterplantdataHis> historydatas = hisWaterPlantDAO.geTWaterplantdataByIdAndOneTime(id, begin);
			//如果有历史数据，则将历史数据加入
			if(historydatas!=null && historydatas.size()>0)
			{
				System.out.println("Data Service historydatas.size()="+historydatas.size());
				for(TWaterplantdataHis historydata:historydatas)
				{
					TWaterplantdata temp = new TWaterplantdata();						
					try {
						PropertyUtils.copyProperties(temp,historydata);
					} catch (IllegalAccessException | InvocationTargetException
							| NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}						
					resutDatas.add(temp);
				}
			}
		}
		else*/ {//只涉及历史表
			List<TWaterplantdataHis> historydatas = hisWaterPlantDAO.geTWaterplantdataByIdAndTwoTime(id,  begin, end);
			//如果有历史数据，则将历史数据加入
			if(historydatas!=null && historydatas.size()>0)
			{
				for(TWaterplantdataHis historydata:historydatas)
				{
					TWaterplantdata temp = new TWaterplantdata();						
					try {
						PropertyUtils.copyProperties(temp,historydata);
					} catch (IllegalAccessException | InvocationTargetException
							| NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}						
					resutDatas.add(temp);
				}
			}
		}	
		return resutDatas;
	}
	
	
	
	/**
	 * 根据时间段得到指定id的指定类型数据
	 * @author wangying
	 * @param id
	 * @param type
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public List<WaterPlantDto> getWaterplantdatasByTwoTime(String id, String type,String beginTime,String endTime)
	{
		//List<TWaterplantdata> resutDatas = new ArrayList<>();
		String begin = beginTime+"/"+"00:00:00";
		String end = endTime +"/"+"24:00:00";
		
		List<WaterPlantDto> historydatas = hisWaterPlantDAO.geTWaterplantdataByIdTypeAndTwoTime(id, type, begin, end);
		//List<TWaterplantdataHis> historydatas = hisWaterPlantDAO.geTWaterplantdataByIdAndTwoTime(id,  begin, end);
		//如果有历史数据，则将历史数据加入
		if(historydatas!=null && historydatas.size()>0)
		{
			System.out.println("WaterPlantDataService historydatas.size()="+historydatas.size());  
				return historydatas;
		
		}	
		return null;
	}
	/**
	 * 保存数据
	 * @author wangying
	 * @param waterplantdataHis
	 * @return
	 */
	public boolean saveWaterPlantData(TWaterplantdataHis waterplantdataHis)
	{
		return hisWaterPlantDAO.save(waterplantdataHis);
	}
	public RealWaterPlantDao getRealWaterPlantDao() {
		return realWaterPlantDao;
	}
	public void setRealWaterPlantDao(RealWaterPlantDao realWaterPlantDao) {
		this.realWaterPlantDao = realWaterPlantDao;
	}
	public HisWaterPlantDAO getHisWaterPlantDAO() {
		return hisWaterPlantDAO;
	}
	public void setHisWaterPlantDAO(HisWaterPlantDAO hisWaterPlantDAO) {
		this.hisWaterPlantDAO = hisWaterPlantDAO;
	}
	
	/*public List<String> getTableColumnName()
	{
		return realWaterPlantDao.getAllColumnName();
	}
	
	public void zuhe()
	{
		TWaterplantdata tWaterplantdata = new TWaterplantdata();
		GetObjectName.zuhe(tWaterplantdata, realWaterPlantDao.getAllColumnName());
	}
*/
}
